<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>个人资料</title>

    <link rel="stylesheet" type="text/css" href="../../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/style.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/yzl.css"/>
</head>
<body style="background-color:#F0F0F0">
<div id="app" v-cloak>
  <div id="blank" class="header shopheader" v-cloak>
      <div v-back.tapmode class="getback"></div>
      <span class="title">商品分类编辑</span>
      <!-- <span tapmode  style="position:absolute;right:10px;font-size:14px;">保存</span> -->
  </div>
  <div class="category_container" v-cloak>
      <div class="category-add" v-open-win vurl="addcategory">+添加分类</div>
      <div class="category_line" v-for="(category, index) in categories" >
          <img class="offline_fill"  v-src="'../../image/offline_fill.png'" v-myclick="[category.shop_category_id, updCategory]">
          <span class="category_name">{{category.name}}</span>
          <img class="category_edit" v-src="'../../image/edit.png'"  v-open-win="{category_id:category.shop_category_id}" vurl="updatecategory">
          <img class="view-list" v-src="'../../image/view-list-o.png'" v-open-win="{category_id:category.shop_category_id}" vurl="categoryinfo">
          <span class="goods_number">{{category.count}}</span>
          <div style="clear:both"></div>
      </div>
  </div>
</div>
</body>
<script src="../../script/api.js"></script>
<script src="../../script/vue.js"></script>
<script src="../../script/app.js"></script>
<script src="../../script/url.js"></script>
<script src="../../script/common.js"></script>
<script src="../../script/components.js"></script>
<script>
var vm,
    categories,

apiready = function () {
  showProgress();
  UIScrollPicture = api.require('UIScrollPicture');
  MNActionButton = api.require('PIActionButton');
  wx = api.require('wx');
  setBSImg();
  var data = {
      member_id: getPrefs('member_id'),
  };
  var callback = function (ret) {
      var home = ret.content;
      categories = home.categories;
      // console.log(JSON.stringify(goods));
      initApp();
      api.parseTapmode();
  };
  pinet.post(URL.getCategoryinfo, data, callback);
  addListeners();
};

function refresh() {
  var data = {
      member_id: getPrefs('member_id')
  };
  var callback = function (ret) {
      var home = ret.content;
      if (vm) {
          vm.categories = home.categories;
          vm.key++;
      } else {
          initApp();
      }
      api.refreshHeaderLoadDone();
  };
  var errback = function () {
      api.refreshHeaderLoadDone();
  };

  pinet.post(URL.getCategoryinfo, data, callback, errback);
}

function initApp() {
    vm = new Vue({
        el: "#app",
        data: {
            categories:categories,
            url: URL.getCategoryinfo,
        },
        methods: {
          updCategory:function(index){
            api.confirm({
                title: '提示',
                msg: "删除分类后，该分类将会在您的店铺中消失",
                buttons: ['确认删除', '再想想']
            }, function (ret, err) {
                var buttonIndex = ret.buttonIndex;
                if (buttonIndex == 1) {
                  showProgress();
                  var data = {
                      member_id: getPrefs('member_id'),
                      category_id:index,
                      type:2
                  };
                  var callback = function (ret) {
                    if(ret.errcode==0){
                      api.sendEvent({
                          name: 'savecategory'
                      });
                    }
                  };
                  pinet.post(URL.saveCategory, data, callback);
                }
            });
          }
        },
    });
}
function addListeners() {
    api.addEventListener({
        name: 'savecategory'
    }, function () {
       refresh();
    });

    api.addEventListener({
        name: 'viewappear'
    }, function () {
        onPageStart();
    });

    api.addEventListener({
        name: 'viewdisappear'
    }, function (ret, err) {
        onPageEnd();
    });

    api.addEventListener({
        name: 'keyback'
    }, function (ret, err) {
        onPageEnd();
        api.closeWin();
    });
}
</script>
</html>
